package swd.work.generator.v1.java.bean;

import java.io.*;
import java.util.*;

import com.pub.database.*;

import swd.work.generator.v1.java.base.*;

public class FlowBean extends BeanExtend implements BeanImplement {
    private String gstrFlowCode = null;


    public FlowBean(QueryBean pqbnDAO, String pstrUserNO) throws Exception {
        super(pqbnDAO, pstrUserNO);
        gstrFlowCode = gstrSysNO.toUpperCase();
    }

    /**
     * outputApproveActionAndCheck
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputApproveActionAndCheck(BufferedWriter pbwrPrgNO,String pstrMasterTable) {
        Hashtable<String,Hashtable<String,Object>> lhtFlowNO = null;
        Hashtable<String,Object> lhtFlowNOData = null;
        int lintFlowNO_cnt = 0;

        try {
            lhtFlowNO = gqbnDAO.hashtableSQL(
                    "select flow_no from floweng_setup where flow_code = '" +
                    gstrFlowCode + "'");

            while (lhtFlowNO != null &&
                   lhtFlowNO.get(String.valueOf(lintFlowNO_cnt)) != null) {

                lhtFlowNOData = (Hashtable<String,Object>) lhtFlowNO.get(String.valueOf(
                        lintFlowNO_cnt));

                lintFlowNO_cnt++;

                outputPreAppFlow(pbwrPrgNO,
                                    lhtFlowNOData.get("FLOW_NO").toString());
                outputAppActionFlow(pbwrPrgNO,
                                   lhtFlowNOData.get("FLOW_NO").toString(),pstrMasterTable);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (lhtFlowNOData != null) {
                lhtFlowNOData.clear();
            }
            lhtFlowNOData = null;
            if (lhtFlowNO != null) {
                lhtFlowNO.clear();
            }
            lhtFlowNO = null;
        }
    }

    /**
     * outputCheckAddRoleMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputAddActionMethod(String pstrMasterTable,BufferedWriter pbwrPrgNO) throws
            Exception {


        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public String addAction(){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrNotNullList = null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrAddStep = null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String lstrInsResult=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"檢查必填欄位\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrNotNullList = gBusiness.checkNotNullFld(gView);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "if (lstrNotNullList.equals(\"\")) {", 2);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"新增存檔\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrInsResult=gBusiness.exeInsSQL(gView);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "if(lstrInsResult.equals(\"\")){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lstrAddStep=\"送出流程\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "(new FlowExpand(\"O\",gqbnDAO.getConnection())).expandFlow(gView.getflow_code(),gView.getflow_num(),\"" + pstrMasterTable + "\",gView.getempno());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}else{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             " return \"新增資料至\" + lstrInsResult + \"發生錯誤!!\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "} else {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return lstrNotNullList + \" 不可為空值\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gView.setPrgStatus(\"null\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return \"資料新增成功並已送出流程!!\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "} catch (Exception e) {", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return \"程式執行至\" + lstrAddStep + \"時發生錯誤!!\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputCheckAppFlow
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrFlowNO String
     */
    private void outputAppActionFlow(BufferedWriter pbwrPrgNO, String pstrFlowNO,String pstrMasterTable) throws
            Exception {

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String appFlow" + pstrFlowNO +
                             "Action() throws Exception{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "callFlowEngApp(gView.getflow_num(),gstrLoginUser,gView.getseqno(),\"" + pstrMasterTable + "\",\"P\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "return \"簽核完畢\";", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "throw new Exception(e.toString());", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputCheckRejectRoleMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputRejectActionMethod(String pstrMasterTable,BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "void rejectAction() throws Exception{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "callFlowEngApp(gView.getflow_num(),gstrLoginUser,gView.getseqno(),\"" + pstrMasterTable + "\",\"X\");", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    private void outputCallFlowEngApp(BufferedWriter pbwrPrgNO) throws Exception{

        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "void callFlowEngApp(String pstrFlowNum,String pstrSemp,String pstrSeq,String pstrTable,String pstrJstFlag) throws Exception{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Hashtable lhtFlowApp= new Hashtable();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"fnum\",pstrFlowNum);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"semp\",pstrSemp);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"seq\",pstrSeq);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"table\",pstrTable);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.put(\"jstflag\",pstrJstFlag);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "(new FlowApprove(\"O\",gqbnDAO.getConnection(),lhtFlowApp)).flowApp();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "throw new Exception(e);", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}finally{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp.clear();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "lhtFlowApp=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

    }

    /**
     * outputClassVarible
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputClassVarible(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "QueryBean gqbnDAO=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String gstrLoginUser=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String gstrFlowCode=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "View gView=null;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "Business gBusiness=null;", 1);
    }

    /**
     * outputContructor
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputContructor(BufferedWriter pbwrPrgNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public Flow(QueryBean pqbnDAO,String pstrLoginUser,String pstrFlowCode,View pview,Business pBusiness){",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gqbnDAO=pqbnDAO;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gstrLoginUser=pstrLoginUser;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gstrFlowCode=pstrFlowCode;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gView=pview;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gBusiness=pBusiness;", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputPreRejectActionMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputPreRejectActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preRejectAction(){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputSetPreAddActionMethod
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputPreAddActionMethod(BufferedWriter pbwrPrgNO) throws
            Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preAddAction(String pstrUserID){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "try{", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gView.setflow_code(gstrFlowCode);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "gView.setflow_num(new GetFlowNum(gqbnDAO.getDBType(),gqbnDAO.getConnection()).getFlowNum(gstrFlowCode));",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gBusiness.setInitValueForAdd(gView,pstrUserID);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "gBusiness.setReadonlyForAdd(gView);",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "catch(Exception e){", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "e.printStackTrace();", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    /**
     * outputSetPreAppFlow
     *
     * @param pbwrPrgNO BufferedWriter
     * @param pstrFlowNO String
     */
    private void outputPreAppFlow(BufferedWriter pbwrPrgNO,
                                     String pstrFlowNO) throws Exception {
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "public void preAppFlow" + pstrFlowNO + "(){",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "//put approve init code in there", 1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);
    }

    public String createBean(String pstrPrgNO) {
        String lstrCreateResult = "";
        String lstrBeanFileName = "Flow.java";
        BufferedWriter lbwrPrgNO = null;

        try {
            lbwrPrgNO = super.getFwrPrgNO(lstrBeanFileName, "jsp_frame_path",
                                          "\\com\\swd\\" + getSysNO(gstrUserNO) +
                                          "\\" + pstrPrgNO + "\\", gstrUserNO);

            super.outputBeanHeader(lbwrPrgNO, super.getSysNO(gstrUserNO),
                                   pstrPrgNO, lstrBeanFileName, gstbTabStr,
                                   gstrTabChar);

            super.setBaseVarible(pstrPrgNO);

            String lstrMasterTable=gqbnDAO.readSingleValueData("select master_table from flow_code where flow_code='" + gstrSysNO.toUpperCase() + "'");

            //output class level varible
            outputClassVarible(lbwrPrgNO);

            //output Constructor
            outputContructor(lbwrPrgNO);

            //output setPreAddAction method.
            outputPreAddActionMethod(lbwrPrgNO);

            //output checkAddRole method
            outputAddActionMethod(lstrMasterTable,lbwrPrgNO);

            //output checkRejectRole method
            outputRejectActionMethod(lstrMasterTable,lbwrPrgNO);

            //output checkRejectRole method
            outputPreRejectActionMethod(lbwrPrgNO);

            outputApproveActionAndCheck(lbwrPrgNO,lstrMasterTable);

            outputCallFlowEngApp(lbwrPrgNO);

            outputReadFlowSeqno(lbwrPrgNO);

            //output class end single
            super.outputJavaCode(lbwrPrgNO, gstbTabStr, gstrTabChar, "}", 1);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                lbwrPrgNO.flush();
                lbwrPrgNO.close();
                lbwrPrgNO = null;
            } catch (Exception se) {
                se.printStackTrace();
                // throw new Exception("can't flush FlowBean");
            }
        }

        return lstrCreateResult;
    }

    /**
     * ouputReadFlowSeqno
     *
     * @param pbwrPrgNO BufferedWriter
     */
    private void outputReadFlowSeqno(BufferedWriter pbwrPrgNO) throws Exception{
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "String readFlowSeqno(View view,String pstrLoginUserNO) throws Exception{",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "return gqbnDAO.readSingleValueData(\"select seqno from wkf_jst_eng where flow_num='\" + view.getflow_num() + \"' and r_empno='\" + pstrLoginUserNO + \"' and jst_flag='N'\");",
                             1);
        super.outputJavaCode(pbwrPrgNO, gstbTabStr, gstrTabChar,
                             "}",
                             1);
    }


}
